package com.fit.pages.product;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.services.PropertyAccess;
import org.apache.tapestry5.util.EnumSelectModel;

import com.fit.annotations.AdminAccess;
import com.fit.annotations.UserAccess;
import com.fit.dao.manufacturer.ManufacturerDAO;
import com.fit.dao.product.ProductDAO;
import com.fit.dao.user.UserDAO;
import com.fit.entities.Manufacturer;
import com.fit.entities.Product;
import com.fit.entities.User;
import com.fit.model.glov.GenericSelectModel;
import com.fit.model.type.ProductType;
import com.fit.model.type.UserType;
import com.fit.model.user.MD5;

@AdminAccess
public class EditProduct {

	@Property
	@Persist
	private Product product;

	@Inject
	private ProductDAO productDAO;

	@Property
	private String newPassword;

	@Component
	private Form editForm;

	@InjectPage
	private ManageProducts manage;

	@Property
	@Persist
	private ProductType type;

	@Inject
	private ManufacturerDAO manufacturerDAO;

	private GenericSelectModel<Manufacturer> manufacturers;

	@Property
	private Manufacturer manufacturer;

	@Inject
	private PropertyAccess access;
	
	@Inject
	private Messages messages;

	public GenericSelectModel<Manufacturer> getManufacturers() {
		manufacturers = new GenericSelectModel<Manufacturer>(manufacturerDAO.findAll(),
				Manufacturer.class, "company", "id", access);
		return manufacturers;
	}

	public SelectModel getTypes() {
		return new EnumSelectModel(ProductType.class, messages);
	}

	Object onActivate(Long id) {
		manage.setError("");
		manage.setConfirmation("");
		Product p = productDAO.findById(id);
		if (p != null) {
			product = p;
		}
		if (product == null) {
			manage.setError("Error");
			return manage;
		}
		return null;
	}

	Object onSubmitFromEditForm() {
		manage.setError("");
		manage.setConfirmation("");
		try {
			
			if (type !=null && !product.getType().equalsIgnoreCase(type.toString())) {
				product.setType(type.toString());
			}
			if (manufacturer !=null && product.getManufacturer() != manufacturer) {
				product.setManufacturer(manufacturer);
			}
			productDAO.update(product);
			manage.setConfirmation("Successfully edited.");
			return manage;
		} catch (Exception e) {
			manage.setError("Error.");
			return manage;
		}
	}
	
	public boolean isManufacturerNotNull(){
		if(product.getManufacturer()!=null){
			return true;
		}
		return false;
	}
	
	@OnEvent(component = "removeCompanyLink")
	Object onRemoveManufacturerLink() {
		product.setManufacturer(null);
		return null;
	}

}
